//
// TextChangedInfo.cs
//
// Author: 
//  Fredy Ramirez <framirez@pacificcoolwave.com>
//
// Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)
//
//  Creation date: Oct 21, 2011
//  Last modification: 

/*
The MIT License

Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */


using System;
using Gtk;

namespace Pcw.PcwTextEditor.PcwDocument
{
	public enum TextChangeType 
	{
		InsertText,
		DeleteText
	}
	
	public class TextChangedInfo
	{
		private TextChangeType change_type;
		private string text;
		private int start_offset;
		private int end_offset;
		private int start_line;
		private int end_line;
		private int start_line_offset;
		private int affected_factor = 1;
		
		public TextChangeType ChangeType {
			get {
				return change_type;
			}
		}
		
		public string Text {
			get {
				return text;
			}
		}
		
		public int StartOffset {
			get {
				return start_offset;
			}
		}
		
		public int EndOffset {
			get {
				return end_offset;
			}
		}		
		
		public int StartLine {
			get {
				return start_line;
			}
		}
		
		public int EndLine {
			get {
				return end_line;
			}
		}

		public int StartLineOffset {
			get {
				return start_line_offset;
			}
		}		
		
		public int LinesAffected {
			get {
				return (end_line - start_line + 1) * affected_factor;
			}
		}
		
		public int CharsAffected {
			get {
				return (end_offset - start_offset) * affected_factor;
			}
		}		
		
		public TextChangedInfo ()
		{
		}
		
		public void SetInfo(TextChangeType changeType, string textChanged, TextIter startIter)
		{
			change_type = changeType;			
			text = textChanged;
			affected_factor = 1;
			if (change_type == TextChangeType.DeleteText)
				affected_factor = -1;
		
			start_offset = startIter.Offset;
			end_offset = start_offset + textChanged.Length;
			
			TextIter startLineIter;
			start_line = startIter.Line;			
			startLineIter = startIter;
			startLineIter.BackwardLine();
			startLineIter.ForwardLine();
			start_line_offset = startLineIter.Offset;
			
			end_line = GetEndLine(textChanged, start_line);
			
			/*
			Console.WriteLine("TextChangedInfo.SetInfo(): Type: '{0}' - StartOffset: '{1}' - EndOffset: '{2}' - CharsAffected: '{4}'" + 
				" - StartLine '{5}' - EndLine '{6}' - LinesAffected '{7}' Text: '{3}'", 
				changeType, StartOffset, EndOffset, text, CharsAffected, StartLine, EndLine, LinesAffected);
			*/
		}
		
		private int GetEndLine(string text, int startLine) 
		{
			if (text.IndexOf("\n") < 0)
				return startLine;			
			
			text = Text.Replace("\r", "");
			
			if(Text.Length == 1 && text.IndexOf("\n") == 0)
				return startLine + 1;
			
			if (text.LastIndexOf("\n") == text.Length - 1)
				text = text.Substring(0, text.LastIndexOf("\n"));
			
			string[] s = text.Split('\n');
			
			if (s.Length == 0)
				return startLine;
			
			return startLine + (s.Length - 1);
		}
	}
}

